package com.lly.tboc.zhouyi

import com.lly.tboc.zhouyi.vo.Gua
import com.lly.tboc.zhouyi.vo.Yao
import java.io.File
import java.io.Reader

fun parseData(file: File): MutableList<Gua> {
    return file.reader().use { parseData(it) }
}

fun parseData(inputStream: Reader): MutableList<Gua> {
    val data = inputStream.readLines()
//            .take(10)
    val guaReg = "第(.*)卦\\s*(.+)\\s+\\S+\\s(.)上(.)下".toRegex()
    val tuanReg = "彖曰：(.+)".toRegex()
    val wenYanReg = "文言曰：(.+)".toRegex()
    val xiangReg = "象曰：(.+)".toRegex()
    val yaoId1Reg = "初.：(.+)象曰：(.+)".toRegex()
    val yaoId2Reg = "上.：(.+)象曰：(.+)".toRegex()
    val yaoId4Reg = "用.：(.+)象曰：(.+)".toRegex()
    val yaoId3Reg = ".(.)：(.+)象曰：(.+)".toRegex()
    var gua: Gua? = null
    val guaList = mutableListOf<Gua>()
    val yaoList = mutableListOf<Yao>()
    for (item in data) {
        guaReg.find(item)?.groupValues?.let {
            gua?.let {
                it.data = yaoList.toList()
                yaoList.clear()
                guaList.add(it)
            }
            gua = Gua(chinese2Num(it[1]), it[2], it[3], it[4], "", "", "", "")

        }
        gua?.apply {
            if (ci.isEmpty() && item.startsWith("$name：")) {
                gua = this.copy(ci = item.substring(2))
            }
        }
        tuanReg.find(item)?.groupValues?.let {
            gua?.apply {
                gua = copy(tuan = it[1])
            }
        }
        if (gua?.xiang?.isEmpty() == true) {
            xiangReg.find(item)?.groupValues?.let {
                gua?.apply {
                    gua = copy(xiang = it[1])
                }
            }
        }
        var isContinue = false
        yaoId1Reg.find(item)?.groupValues?.let {
            yaoList.add(Yao(1, gua!!.id, item.substring(0, 2), it[1], it[2], ""))
            isContinue = true
        }
        if (isContinue) {
            continue
        }
        yaoId2Reg.find(item)?.groupValues?.let {
            yaoList.add(Yao(6, gua!!.id, item.substring(0, 2), it[1], it[2], ""))
            isContinue = true
        }
        if (isContinue) {
            continue
        }
        yaoId4Reg.find(item)?.groupValues?.let {
            yaoList.add(Yao(7, gua!!.id, item.substring(0, 2), it[1], it[2], ""))
            isContinue = true
        }
        if (isContinue) {
            continue
        }
        yaoId3Reg.find(item)?.groupValues?.let {
            yaoList.add(
                Yao(chinese2Num(it[1]), gua!!.id, item.substring(0, 2), it[2], it[3], "")
            )
        }

        wenYanReg.find(item)?.groupValues?.let {
            gua!!.wenyan = item
            isContinue = true
        }
        if (isContinue) {
            continue
        }
        gua?.wenyan?.let {
            if (it.isNotEmpty()) {
                gua!!.wenyan += item + "\n"
            }
        }

    }
    gua?.let {
        it.data = yaoList.toList()
        yaoList.clear()
        guaList.add(it)
    }

    return guaList
}

fun chinese2Num(s: String): Int {
    var ans = 0
    for (c in s) {
        when (c) {
            '一' -> {
                ans += 1
            }

            '二' -> {
                ans += 2
            }

            '三' -> {
                ans += 3
            }

            '四' -> {
                ans += 4
            }

            '五' -> {
                ans += 5
            }

            '六' -> {
                ans += 6
            }

            '七' -> {
                ans += 7
            }

            '八' -> {
                ans += 8
            }

            '九' -> {
                ans += 9
            }

            '十' -> {
                ans *= 10
                if (ans == 0) {
                    ans = 10
                }
            }

            else -> {}
        }
    }
    return ans
}
